﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Magic.Core;

public class GZipHelper
{
    /// <summary>
    /// GZip压缩
    /// </summary>
    /// <param name="rawData"></param>
    /// <returns></returns>
    public static byte[] Compress(byte[] rawData)
    {
        using MemoryStream ms = new MemoryStream();
        using GZipStream compressedStream = new GZipStream(ms, CompressionMode.Compress, true);
        compressedStream.Write(rawData, 0, rawData.Length);
        compressedStream.Close();
        return ms.ToArray();
    }

    /// <summary>
    /// ZIP解压
    /// </summary>
    /// <param name="zippedData"></param>
    /// <returns></returns>
    public static byte[] Decompress(Stream ms)
    {
        using var compressedStream = new GZipStream(ms, CompressionMode.Decompress);
        using MemoryStream outBuffer = new MemoryStream();
        byte[] block = new byte[1 << 20];
        while (true)
        {
            int bytesRead = compressedStream.Read(block, 0, block.Length);
            if (bytesRead <= 0)
                break;
            else
                outBuffer.Write(block, 0, bytesRead);
        }
        compressedStream.Close();
        return outBuffer.ToArray();
    }

    public int GetStringSize(string str)
    {
        int size = 0;
        for (int i = 0; i < str.Length; i++)
        {
            size++;
        }
        return size;
    }
}
